package com.hxh.generater.springBoot.springboot3_vue3.gen.crud;

import com.hxh.entity.Project;
import com.hxh.entity.ProjectDetail;
import com.hxh.entity.TemplateConfig;
import com.hxh.entity.po.ClassInfo;
import com.hxh.generater.common.remote.SpringbootRemoteUtil;
import com.hxh.generater.springBoot.springboot3_vue3.gen.GenSb3Vue3Util;
import com.hxh.util.GenUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;

@Slf4j
public class GenServiceRemote {

    public static void generationServiceClass(Project project, ClassInfo classInfo) throws IOException {
        generationServiceClass(project, classInfo, true);
    }

    public static String generationServiceClass(String packageName, ClassInfo classInfo, TemplateConfig templateConfig) throws IOException {
        Project project = new Project();
        ProjectDetail backendProject = new ProjectDetail();
        backendProject.setProjectGroup(packageName);
        project.setBackendProject(backendProject);
        project.setTemplateConfig(templateConfig);
        return generationServiceClass(project, classInfo, false);
    }

    private static String generationServiceClass(Project project, ClassInfo classInfo, boolean writeFile) throws IOException {
        String className = classInfo.getClassName();
        String serviceName = className + "Service";

        SpringbootRemoteUtil springbootRemoteUtil = new SpringbootRemoteUtil();
        springbootRemoteUtil.setProject(project);
        springbootRemoteUtil.setClassInfo(classInfo);
        springbootRemoteUtil.setWriteFile(writeFile);
        springbootRemoteUtil.setRemoteUrlSuffix("springboot/service");
        springbootRemoteUtil.setWriteFileName(serviceName);
        String mutilDatasourcePathName = GenUtil.getMutilDatasourceStrByConfig(project, classInfo, "", File.separator);
        springbootRemoteUtil.setWriteFilePath("service\\" + mutilDatasourcePathName);
        String projectNamePath = "";
        if (project != null) {
            projectNamePath = project.getName() + "\\";
        }
        String classNameEntity = StringUtils.uncapitalize(className);
        springbootRemoteUtil.setAdditionFilePath(GenSb3Vue3Util.FILE_PATH_PRE + "addition\\"+projectNamePath+"service\\" + classNameEntity);
        return springbootRemoteUtil.process();
    }

    public static void generationServiceImplClass(Project project, ClassInfo classInfo) throws IOException {
        generationServiceImplClass(project, classInfo, true);
    }

    public static String generationServiceImplClass(String packageName, ClassInfo classInfo, TemplateConfig templateConfig) throws IOException {
        Project project = new Project();
        ProjectDetail backendProject = new ProjectDetail();
        backendProject.setProjectGroup(packageName);
        project.setBackendProject(backendProject);
        project.setTemplateConfig(templateConfig);
        return generationServiceImplClass(project, classInfo, false);
    }

    private static String generationServiceImplClass(Project project, ClassInfo classInfo, boolean writeFile) throws IOException {
        String className = classInfo.getClassName();
        String serviceName = className + "ServiceImpl";

        SpringbootRemoteUtil springbootRemoteUtil = new SpringbootRemoteUtil();
        springbootRemoteUtil.setProject(project);
        springbootRemoteUtil.setClassInfo(classInfo);
        springbootRemoteUtil.setWriteFile(writeFile);
        springbootRemoteUtil.setRemoteUrlSuffix("springboot/serviceImpl");
        springbootRemoteUtil.setWriteFileName(serviceName);
        String mutilDatasourcePathName = GenUtil.getMutilDatasourceStrByConfig(project, classInfo, "", File.separator);
        springbootRemoteUtil.setWriteFilePath("service\\impl\\" + mutilDatasourcePathName);
        String projectNamePath = "";
        if (project != null) {
            projectNamePath = project.getName() + "\\";
        }
        String classNameEntity = StringUtils.uncapitalize(className);
        springbootRemoteUtil.setAdditionFilePath(GenSb3Vue3Util.FILE_PATH_PRE + "addition\\"+projectNamePath+"serviceImpl\\" + classNameEntity);
        return springbootRemoteUtil.process();
    }

}
